<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BItemManager
 *
 * @author jianfeng
 */
class BTreeItemManager extends BEntity {

    protected $owner;
    protected $ownerType;
    protected $ownerId;
    protected $item;
    protected $itemClass;
    protected $itemId;
    protected $isLoaded = false;
    protected $items = array();
    protected $itemsByName = array();

    public function __construct($owner = NULL, $item = NULL) {

        if ($owner instanceof BEntity && $owner->getId() != NULL) {
            $this->owner = $owner;
            $this->ownerType = get_class($owner);
            $this->ownerId = $owner->getId();
        }

        if ($item instanceof BTreeItem) {
            $this->itemClass = get_class($item);
            $this->item = $item;
            if ($item->getId() != NULL) {
                $this->itemId = $item->getId();
            }
        }
        //parent::__construct($item, $context);
    }

  

    public function getModelClass($data = NULL) {
        /*
          $listModelClass = $this->owner->getModelClass();
          $patten = "/^([A-Z]+)/";
          preg_match($patten, $this->itemClass, $matches);
          $firstCaptalLetters = $matches[0];
          $length = strlen($firstCaptalLetters);
          $lastCaptialLetter = substr($firstCaptalLetters, $length - 1);

          $modfiedItemClass = preg_replace($patten, $lastCaptialLetter, $this->itemClass);

          $modelClass = $listModelClass . $modfiedItemClass;

          return $modelClass;
         * 
         */
        return $this->item->getModelClass();
    }

    public function getRepository($data = NULL, $reload = FALSE) {
        $repository = parent::getRepository($data, $reload);
        $repository->setModel($this->getModelClass());
        return $repository;
    }

    public function getItems($data = NULL) {
        $this->loadItems($data);
        return $this->items;
    }

    public function addItem($itemData) {
        if (is_array($itemData)) {
            $itemData['ownerType'] = $this->ownerType;
            $itemData['ownerId'] = $this->ownerId;
        }
        $itemClass = $this->itemClass;
        if ($itemClass != NULL && $itemData instanceof $itemClass) {
            $itemData->ownerType = $this->ownerType;
            $itemData->ownerId = $this->ownerId;
        }
        $findAttributes = $this->getRepository()->populateFindContext($$itemData);
        $row = $this->getRepository()->getRow($findAttributes);
        if (!$row instanceof BRow || $row->getId() == NULL) {
            $row = $this->getRepository()->getNewRow();
            $attributes = $this->getRepository()->populateAttributeList($itemData);
            $row->attributes = $attributes;
            $row->save();
        }
        //if ($row instanceof BRow && $row->getId() != NULL) {
        //return $this->getRepository()->loadEntity($row);
        //}
    }

    public function removeItem($itemData) {

        $itemId = NULL;

        if ($itemData instanceof BEntity && $itemData->getId() != NULL)
            $itemId = $itemData->getId();

        if (is_numeric($itemData) && $itemData > 0) {
            $itemId = $itemData;
            if ($this->itemClass != NULL && $itemId != NULL) {
                $itemClass = $this->itemClass;
                $itemClass::delete($itemId);
                //reload items
                $this->loadItems();
            }
        }
    }

    public function getItemById($id, $context = NULL) {
        $this->loadItems($context);
        if (isset($this->items[$id]))
            return $this->itmes[$id];
        return FALSE;
    }

    public function getItemByName($name, $context = NULL) {
        $this->loadItems($context);
        if ($this->itemsByName == NULL) {
            foreach ($this->items as $item) {
                $this->itemsByName[$item->getName()] = $item;
            }
        }
        if (isset($this->itemsByName[$name])) {
            return $this->itemsByName[$name];
        }
        return FALSE;
    }

    protected function loadItems($itemData = NULL) {

        if (!$this->isLoaded) {
            if (is_array($itemData)) {
                $itemData['ownerType'] = $this->ownerType;
                $itemData['ownerId'] = $this->ownerId;
            }
            $itemClass = $this->itemClass;
            if ($itemClass != NULL && $itemData instanceof $itemClass) {
                $itemData->ownerType = $this->ownerType;
                $itemData->ownerId = $this->ownerId;
            }
            $findAttributes = $this->getRepository()->populateFindContext($$itemData);

            $view = $this->getRepository()->getView($findAttributes);
            $rows = $view->rows;
            foreach ($rows as $id => $row) {
                $itemClass = $this->itemClass;
                $item = new $itemClass();
                $item->getRepository()->loadEntity($row);
                $this->items[$id] = $item;
            }
            $this->isLoaded = TRUE;
        }
    }

}
